package com.example.featuresdk.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.example.featuresdk.core.AbstractFeatureSDK
import com.example.featuresdk.navigation.NavigationController
import com.example.featuresdk.theme.DemoTheme
import java.util.stream.Collectors

@Composable
inline fun FeatureEntry(icon: Int, crossinline onClick:()->Unit) {

    Spacer(modifier = Modifier.width(2.dp))
    Image(painter = painterResource(icon),
        contentDescription = "",
        modifier = Modifier
            .width(30.dp)
            .height(30.dp)
            .clickable { onClick() }
    )
    Spacer(modifier = Modifier.width(2.dp))
}

@Composable
fun FeatureEntryPlaceholder() {
    Spacer(modifier = Modifier.width(34.dp))
}

@Composable
inline fun FeatureEntry(label: String, crossinline onClick:()->Unit) {

    Spacer(modifier = Modifier.width(2.dp))
    Text(text = label,
        modifier = Modifier
            .width(30.dp)
            .height(30.dp)
            .clickable { onClick() }
    )
    Spacer(modifier = Modifier.width(2.dp))
}

//@OptIn(ExperimentalUnitApi::class)
@Composable
fun BottomFeatureEntry(selected: Boolean = false, featureSDK: AbstractFeatureSDK) {
    Column (verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally){

        Image(painter = painterResource(featureSDK.featureIcon(selected)),
            contentDescription = "",
            modifier = Modifier
                .width(30.dp)
                .height(30.dp)
                .drawWithContent {
                    drawContent()

                }
        )
//        Text(text = featureSDK.label(),
//            color = if (selected) Color.Red else Color.Black,
//            fontSize = TextUnit(3f, TextUnitType.Em),
//            fontStyle = FontStyle.Normal,
//            fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal
//        )

        Text(text = featureSDK.featureDesc(),
            color = contentColorFor(DemoTheme.colors.onPrimary),
            style = if(selected) DemoTheme.typography.featureEntrySelected
                    else DemoTheme.typography.featureEntryNormal)
    }
}

@Composable
inline fun AppBottomNavigationView(heightDp:Int, bottomContent :@Composable () -> Unit){

    Row(modifier = Modifier.background(DemoTheme.colors.primary).height(heightDp.dp).fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically) {

        bottomContent()
    }
}

@Composable
fun AppBottomNavigationView(selectedFeatureKey: String, featureSDKs: List<AbstractFeatureSDK> = NavigationController.instance.featureSDKs){

    if(featureSDKs.isEmpty()){
        return
    }
    val featureKeys = remember {
        featureSDKs.stream().map { it.featureKey() }.collect(Collectors.toList())
    }

    TabRow(selectedTabIndex = featureKeys.indexOf(selectedFeatureKey),
        modifier = Modifier.height(ScreenConstraints.appBottomBarHeightDp.dp).fillMaxWidth(),
        backgroundColor = DemoTheme.colors.primary,
        contentColor = contentColorFor(DemoTheme.colors.primary)) {

        featureSDKs.forEach { feature ->
            Tab(selected = false,
                modifier = Modifier.width(40.dp),
                onClick = {
                    //nothing to do when clicking the current tab (selected)
                    if(selectedFeatureKey == feature.featureKey()){
                        return@Tab
                    }
                    NavigationController.instance.navigate(feature.featureKey())
                })
            {
                BottomFeatureEntry(selectedFeatureKey == feature.featureKey(), feature)
            }
        }
    }
}


//@Composable
//fun AppBottomFeatureSelector(navigationViewModel: NavigationViewModel, navController: NavHostController){
//
//    //back navigation: update the selected tab index when system back is triggered or when startup
//    val navBackStackEntry by navController.currentBackStackEntryAsState()
//    val backEntryRoute = navBackStackEntry?.arguments?.get("android-support-nav:controller:deepLinkIntent")
////        Log.i("***backEntryRoute****","${navBackStackEntry?.arguments}")
//    //Bundle[{android-support-nav:controller:deepLinkIntent=Intent { dat=android-app://androidx.navigation/Book }}]
//
//    var currentEntryRoute:String? = navController.graph.startDestinationRoute
//
//    //backEntryRoute is null when the app starts up
//    backEntryRoute?.let { currentEntryRoute = (it as Intent).dataString?.replace("android-app://androidx.navigation/","") }
//
////        Log.i("***currentEntryRoute****","$currentEntryRoute")
//
//    currentEntryRoute?.let {
//        navigationViewModel.primaryFeatures.forEachIndexed { index, feature ->
//            if(currentEntryRoute!!.startsWith(feature.featureKey())) {
//                navigationViewModel.selectedPrimaryFeatureTabIndex.value = index
//                navigationViewModel.selectedPrimaryFeatureTitle.value = feature.label()
//                navigationViewModel.activeContextualFeatureKey.value = feature.featureKey()
//            }
//        }
//    }
//}